package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.pojo.SysRole;
import com.ktjy.pojo.SysUser;
import com.ktjy.service.ISysRoleService;
import com.ktjy.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-01-09
 */
@Controller
@RequestMapping("/user")
public class SysUserController {
    Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    ISysUserService iSysUserService;
    @Autowired
    ISysRoleService iSysRoleService;

    @RequestMapping("/list")
    public String list(SysUser sysUser,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       Model model) {
        try {
            IPage<SysUser> pager = iSysUserService.findPage(sysUser, pageIndex, pageSize);
            model.addAttribute("pager", pager);

            //回显数据
            model.addAttribute("condition", sysUser);
        } catch (Exception e) {
            logger.error("SysUserController 中的 list 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "user/list";
    }

    /**
     * 跳转到新增页面
     *
     * @return
     */
    @GetMapping("/add")
    public String add(Model model) {
        try {
            //查询所有角色
            List<SysRole> roles = iSysRoleService.list();
            model.addAttribute("roles", roles);
            return "user/add";
        } catch (Exception e) {
            logger.error("SysUserController 中的 add 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/save")
    public String save(SysUser sysUser) {
        try {
            boolean ok = iSysUserService.save(sysUser);
            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/user/list";
            } else {
                //跳回新增页面
                return "user/add";
            }
        } catch (Exception e) {
            logger.error("SysUserController 中的 save 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 跳转修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit")
    public String edit(@RequestParam("id") Long id, Model model) {
        try {
            SysUser user = iSysUserService.getById(id);
            model.addAttribute("user", user);

            //查询所有角色
            List<SysRole> roles = iSysRoleService.list();
            model.addAttribute("roles", roles);
        } catch (Exception e) {
            logger.error("SysUserController 中的 toEdit 方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "user/edit";
    }

    @PostMapping("/update")
    public String update(SysUser sysUser) {
        try {
            boolean ok = iSysUserService.updateById(sysUser);

            if (ok) {
                //跳转到列表;重定向到分页方法
                return "redirect:/user/list";
            } else {
                //跳回新增页面
                return "user/edit";
            }
        } catch (Exception e) {
            logger.error("SysUserController 中的 update 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
    }


    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Long id) {
        Map<String, Object> map = new HashMap<>();
        try {
            boolean ok = iSysUserService.removeById(id);  //调用service的方法进行数据删除
            if (ok) {
                map.put("delResult", true);
            } else {
                map.put("delResult", false);
            }
        } catch (Exception e) {
            logger.error("SysUserController 中的 del 方法执行发生错误。", e);
            map.put("delResult", false);
        }
        return map;
    }

}
